package com.draby.starfantasymaze.activities.main.adapter;

import android.content.Context;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.draby.starfantasymaze.R;
import com.draby.starfantasymaze.dto.Achievement;
import com.draby.starfantasymaze.utils.DateFormatUtil;
import com.draby.starfantasymaze.utils.FileUtil;

import java.text.SimpleDateFormat;
import java.util.List;

public class AchievementAdapter extends RecyclerView.Adapter<AchievementAdapter.AchievementViewHolder> {

    private final Context context;
    private final List<Achievement> achievements;

    public AchievementAdapter(Context context, List<Achievement> achievements) {
        this.context = context;
        this.achievements = achievements;
    }

    @NonNull
    @Override
    public AchievementViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AchievementViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_achievement_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AchievementViewHolder holder, int position) {
        SimpleDateFormat formatter = DateFormatUtil.getDefaultFormatter1();
        Achievement achievement = achievements.get(position);
        // 成就的图片ID，名称和简述
        Integer PID = achievement.getPictureID();
        String name = achievement.getName();
        String resume = achievement.getResume();
        // 判断成就完成状态，对成就进行处理
        if (achievement.getOwned()) {
            // 成就已达成
            holder.ll_gainTime.setVisibility(View.VISIBLE);
            holder.tv_achievement_gainTime.setText(formatter.format(achievement.getGainTime()));
            holder.iv_achievement.setColorFilter(null);
        } else {
            // 成就未达成，设置图片饱和度为0，获取时间不可见
            ColorMatrix cm = new ColorMatrix();
            cm.setSaturation(0);
            ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
            holder.iv_achievement.setColorFilter(filter);
            holder.ll_gainTime.setVisibility(View.GONE);
            // 判断成绩是否为隐藏成就
            if (achievement.getHidden()) {
                PID = R.drawable.achi_hidden;
                name = "隐藏成就";
                resume = "解锁后才可查看哦~";
            }
        }
        // 根据 图片ID 设置成就图片
        holder.iv_achievement.setImageResource(PID);
        holder.tv_achievement_name.setText(name);
        holder.tv_achievement_resume.setText(resume);
    }

    @Override
    public int getItemCount() {
        return achievements == null ? 0 : achievements.size();
    }

    static class AchievementViewHolder extends RecyclerView.ViewHolder {

        private final LinearLayout ll_gainTime;
        private final ImageView iv_achievement;
        private final TextView tv_achievement_name;
        private final TextView tv_achievement_resume;
        private final TextView tv_achievement_gainTime;

        public AchievementViewHolder(@NonNull View itemView) {
            super(itemView);
            // 找到控件
            ll_gainTime = itemView.findViewById(R.id.ll_gainTime);
            iv_achievement = itemView.findViewById(R.id.iv_achievement);
            tv_achievement_name = itemView.findViewById(R.id.tv_achievement_name);
            tv_achievement_resume = itemView.findViewById(R.id.tv_achievement_resume);
            tv_achievement_gainTime = itemView.findViewById(R.id.tv_achievement_gainTime);
        }
    }
}
